package com.zuimei.zuimei.fragments;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.adapters.EveryDpagerAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.EveryDBean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.EveryDJson;
import com.zuimei.zuimei.utils.ConstantUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 每日最美
 */

public class EverydayFragment extends BaseFragment implements ViewPager.OnPageChangeListener {
    @Bind(R.id.tv_ed_time)
    TextView tvEdTime;
    @Bind(R.id.linear_ed_time)
    LinearLayout linearEdTime;
    @Bind(R.id.tv_ed_time_hao)
    TextView tvEdTimeHao;
    @Bind(R.id.tv_ed_time_yue)
    TextView tvEdTimeYue;
    @Bind(R.id.tv_ed_time_xqi)
    TextView tvEdTimeXqi;
    @Bind(R.id.linear_ed_time1)
    LinearLayout linearEdTime1;
    @Bind(R.id.relat_ed_time)
    LinearLayout relatEdTime;
    @Bind(R.id.viewpager_ed)
    ViewPager viewpagerEd;
    private List<EveryDBean.DataBean.AppsBean> list;
    private String[] time={"今天","昨天"};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_everyday;
    }

    @Override
    public void init() {

    }

    @Override
    public void loadData() {
        new HttpHelper(mActivity).getData(ConstantUtil.everyDayUrl, new HttpHelper.OnLoadCompletedListener() {
            @Override
            public void onSuccess(String result) {
                EveryDBean bean = EveryDJson.getEveryDBeanJson(result);
                list = bean.getData().getApps();
                setViewPager(list);

            }

            @Override
            public void onFailure() {
                Log.e("22222", "onfa: --->");
            }
        });

    }

    private void setViewPager(List<EveryDBean.DataBean.AppsBean> data) {
        PagerAdapter adapter=new EveryDpagerAdapter(getChildFragmentManager(), data);
        viewpagerEd.setAdapter(adapter);
        viewpagerEd.addOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position==0||position==1){
            linearEdTime.setVisibility(View.VISIBLE);
            relatEdTime.setVisibility(View.GONE);
            tvEdTime.setText(time[position]);
        }else {
            linearEdTime.setVisibility(View.GONE);
            relatEdTime.setVisibility(View.VISIBLE);
            String[] times = list.get(position).getPublish_date().split("-");
            tvEdTimeHao.setText(times[2]);
            tvEdTimeYue.setText(Integer.parseInt(times[1])+"");
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
